package com.hcxy.filetransform.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import com.hcxy.filetransform.Constant;
import com.hcxy.filetransform.R;
import com.hcxy.filetransform.domain.ArrayMap;
import com.hcxy.filetransform.domain.DeviceInfo;
import com.hcxy.filetransform.domain.FileStateCacheLog;
import com.hcxy.filetransform.domain.FileTranslateInfo;
import com.hcxy.filetransform.domain.TransformLog;
import com.hcxy.filetransform.util.FileUtils;
import com.hcxy.filetransform.widget.CircleImage;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class TransformLogAdapter extends BaseAdapter {

	private Context context;
	//private List<TransformLog> datas;
	private ArrayMap<String,TransformLog> datas;
	public TransformLogAdapter(Context context,ArrayMap<String,TransformLog> datas) {
		this.context = context;
		datas.reverse();
		this.datas = datas;
	}
	
	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}
	
	public TransformLog getItem(String key){
		return datas.get(key);
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void addData(ArrayMap<String,TransformLog> newDatas,boolean old){
		
		
		if (old) {
			newDatas.reverse();
			this.datas.addAll(0, newDatas);
		}else{
			this.datas.addAll(newDatas);
		}
	}
	
	
	
	public void addData(String key,TransformLog newData,boolean old){
		//this.datas.add(newData);
		if (old) {
			this.datas.put(0,key,newData);
		}else{
			this.datas.put(key,newData);
		}
	}

	class DateSortCompator implements Comparator<TransformLog>{
		@Override
		public int compare(TransformLog lhs, TransformLog rhs) {
			int flag = lhs.getDate().compareTo(rhs.getDate())*-1;
			return flag;
		}
		
	}
	
	@SuppressLint("NewApi")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MyTransformLogHolder holder=null;
		TransformLog tlog = datas.get(position);
		if (null==convertView) {
			holder = new MyTransformLogHolder();
			if (Constant.FLAG_POST==tlog.getIspost()) {
				convertView = LayoutInflater.from(context).inflate(R.layout.item_my_transform_log, null);
			}else{
				convertView = LayoutInflater.from(context).inflate(R.layout.item_another_transform_log, null);
			}
			ViewUtils.inject(holder, convertView);
			convertView.setTag(holder);
		}else{
			holder = (MyTransformLogHolder) convertView.getTag();
		}
		int msgType = tlog.getType();
		if (Constant.TYPE_PLAIN_TEXT==msgType) {
			holder.txMsg.setText(tlog.getData());
			holder.imgMsg.setVisibility(View.GONE);
			holder.fileProgress.setVisibility(View.GONE);
		}else if(Constant.TYPE_IMAGE==msgType){
			/*
			FileTranslateInfo finfo = FileUtils.getFileInfo(tlog.getData());
			if (null!=finfo) {
				holder.txMsg.setText(finfo.getFileName());
			}else{
			}
			*/
			//holder.txMsg.setText(tlog.getFileName());
			if (tlog.isFinish()) {
				holder.imgMsg.setVisibility(View.VISIBLE);
				holder.fileProgress.setVisibility(View.GONE);
				holder.imgMsg.setBackground(Drawable.createFromPath(tlog.getData()));
			}else{
				holder.imgMsg.setVisibility(View.GONE);
				holder.fileProgress.setVisibility(View.VISIBLE);
				holder.fileProgress.setMax((int)(tlog.getFilelength()));
				holder.fileProgress.setProgress((int)(tlog.getProgress()));
			}
		}else if (Constant.TYPE_PLAIN_FILE==msgType) {
			/*FileTranslateInfo finfo = FileUtils.getFileInfo(tlog.getData());
			if (null!=finfo) {
				holder.txMsg.setText(finfo.getFileName());
			}else{
			}*/
			holder.txMsg.setText(tlog.getFileName());
			if (tlog.isFinish()) {
				holder.imgMsg.setVisibility(View.GONE);
				holder.fileProgress.setVisibility(View.GONE);
				//holder.imgMsg.setBackground(Drawable.createFromPath(tlog.getData()));
			}else{
				holder.imgMsg.setVisibility(View.GONE);
				holder.fileProgress.setVisibility(View.VISIBLE);
				holder.fileProgress.setMax((int)(tlog.getFilelength()));
				holder.fileProgress.setProgress((int)(tlog.getProgress()));
			}
		}
		int imgid = R.drawable.android;
		int fromdevicetype = tlog.getFromdevicetype();
		if (DeviceInfo.DeviceType.ANDROID==fromdevicetype) {
			imgid = R.drawable.android;
		}else if (DeviceInfo.DeviceType.COMPUTER==fromdevicetype) {
			imgid = R.drawable.computer;
		}
		holder.fromImg.setBackgroundResource(imgid);
		holder.txTime.setText(parseDate(tlog.getDate()));
		return convertView;
	}
	
	private class MyTransformLogHolder{
		@ViewInject(R.id.log_from_img)
		public CircleImage fromImg;
		@ViewInject(R.id.log_text_msg)
		public TextView txMsg;
		@ViewInject(R.id.log_img_msg)
		public ImageView imgMsg;
		@ViewInject(R.id.log_time)
		public TextView txTime;
		@ViewInject(R.id.file_progress)
		public ProgressBar fileProgress;
	}
	
	public String parseDate(Date date){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}

	public void updateLogValue(String key,TransformLog log) {
		datas.updateVaule(key, log);
	}

}
